package com.anstudio.busfetion;


import java.util.ArrayList;
import java.util.List;

import com.anstudio.busfetion.model.FavoriteItem;
import com.anstudio.busfetion.util.LocalUtil;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.ListView;

public class FavoriteActivity extends ListActivity {
	
	
	public static void launchForResult(Activity context, int requestCode){
		Intent intent = new Intent(context, FavoriteActivity.class);
		context.startActivityForResult(intent, requestCode);
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.favorite_list);
		getListView().setEmptyView(findViewById(R.id.empty_text));
		FavoriteAdapter adapter = new FavoriteAdapter(this, getFavorite());
		getListView().setAdapter(adapter);
	}
	
	private List<FavoriteItem> getFavorite(){
		LocalUtil util = new LocalUtil();
		List<FavoriteItem> list = new ArrayList<FavoriteItem>();
		String[] strings = util.localLoadFav();
		FavoriteItem item = null;
		for(int i=0;i<strings.length;i++){
			if(i%2==0){
				item = new FavoriteItem();
				item.setBus(strings[i]);
			}else{
				item.setStation(strings[i]);
				list.add(item);
			}
		}
		return list;
	}
	
	public class FavoriteAdapter extends BaseAdapter{
		private List<FavoriteItem> favorites;
		private Context mContext;
		
		public FavoriteAdapter(Context context, List<FavoriteItem> favorites){
			mContext = context;
			this.favorites = favorites;
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			if(favorites==null)
				return 0;
			else
				return favorites.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return favorites.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			FavoriteItem item = (FavoriteItem) getItem(position);
			View view = convertView;
			if(view==null){
				LayoutInflater inflater = 
					(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				view = inflater.inflate(R.layout.favorite_item, null);
			}
			TextView busLine = (TextView) view.findViewById(R.id.bus_line_text);
			busLine.setText(item.getBus());
			TextView busStation = (TextView) view.findViewById(R.id.bus_station_text);
			busStation.setText(item.getStation());
			return view;
		}
		
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		FavoriteItem item = (FavoriteItem) l.getAdapter().getItem(position);
		Intent intent = new Intent();
		intent.putExtra("bus", item.getBus());
		intent.putExtra("station", item.getStation());
		setResult(RESULT_OK, intent);
		finish();
	}
	
	
}
